package 刷题.leedcode36;
//栈
import java.util.Stack;

public class PalindromeList2 {
    public boolean chkPalindrome(ListNode A){
        Stack<Integer> stack = new Stack<>();
        ListNode B = A;
        int count1 = 0;
        int count2 = 0;
        //计算链表长度
        while(B != null){
            count1 ++;
            B = B.next;
        }
        count1 /= 2;
        count2 = count1;
        //遍历一半链表，将链表元素入栈
        while(count1 > 0){
            count1 --;
            stack.push(A.val);
            A = A.next;
        }
        while(count2 > 0){
            count2 --;
            if(stack.pop() == A.val){
                A = A.next;
            }else {
                return false;
            }
        }
        return true;
    }
}
